home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / transpor / ifmail23.z / ifmail23 / ifmail / iflib / execsh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-04  |  1.1 KB  |  60 lines

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/wait.h>
  8. #include "lutil.h"
  9.  
  10. #define SHELL "/bin/sh"
  11.  
  12. int execsh(cmd,in,out,err)
  13. char *cmd,*in,*out,*err;
  14. {
  15.     int pid,status,rc;
  16.  
  17.     loginf("Execute command: %s",cmd);
  18.     fflush(stdout);
  19.     fflush(stderr);
  20.     if ((pid=fork()) == 0)
  21.     {
  22.         if (in)
  23.         {
  24.             close(0);
  25.             if (open(in,O_RDONLY) != 0)
  26.             {
  27.                 logerr("$Reopen of stdin to %s failed",in);
  28.                 exit(-1);
  29.             }
  30.         }
  31.         if (out)
  32.         {
  33.             close(1);
  34.             if (open(out,O_WRONLY | O_APPEND | O_CREAT,0600) != 1)
  35.             {
  36.                 logerr("$Reopen of stdout to %s failed",out);
  37.                 exit(-1);
  38.             }
  39.         }
  40.         if (err)
  41.         {
  42.             close(2);
  43.             if (open(err,O_WRONLY | O_APPEND | O_CREAT,0600) != 2)
  44.             {
  45.                 logerr("$Reopen of stderr to %s failed",err);
  46.                 exit(-1);
  47.             }
  48.         }
  49.         rc=execl(SHELL,"sh","-c",cmd,NULL);
  50.         logerr("$Exec \"%s\" returned %d",cmd,rc);
  51.         exit(-1);
  52.     }
  53.     while (((rc=wait(&status)) != pid) && ((rc=wait(&status)) != 0))
  54.         logerr("$Wait returned %d, status %d,%d",rc,status>>8,status&0xff);
  55.     if ((status&0xff) == 0) rc=status>>8;
  56.     else rc=status&0xff;
  57.     debug(2,"rc=%d",rc);
  58.     return rc;
  59. }
  60.